import {loginByUsername, logout, getUserInfo} from '@/api/login'
import {getToken, setToken, removeToken} from '@/utils/auth'

const user = {
  state: {
    user: '',
    status: '',
    code: '',
    token: getToken(),
    name: '',
    avatar: '',
    introduction: '',
    // roles: [getRoles()],
    roles: [],
    setting: {
      articlePlatform: []
    }
  },

  mutations: {
    SET_CODE: (state, code) => {
      state.code = code
    },
    SET_TOKEN: (state, token) => {
      state.token = token
    },
    SET_INTRODUCTION: (state, introduction) => {
      state.introduction = introduction
    },
    SET_SETTING: (state, setting) => {
      state.setting = setting
    },
    SET_STATUS: (state, status) => {
      state.status = status
    },
    SET_NAME: (state, name) => {
      state.name = name
    },
    SET_AVATAR: (state, avatar) => {
      state.avatar = avatar
    },
    SET_ROLES: (state, roles) => {
      state.roles = roles
    }
  },

  actions: {
    // 用户名登录
    LoginByUsername({commit}, userInfo) {
      const username = userInfo.username.trim()
      return new Promise((resolve, reject) => {
        loginByUsername(username, userInfo.password).then(response => {
          const data = response
          if (data.status === 401) {
            commit('SET_TOKEN', '')
            setToken('')
          } else {
            commit('SET_TOKEN', data.data.cookie)
            // commit('SET_ROLES', ['' + data.data.position_id])
            // commit('SET_ROLES',data.data.rolename)
            setToken(data.data.cookie)
            // setToken(data.data.cookie)
            // setToken(data.data.cookie)
            // this.GetUserInfo()
          }
          resolve()
        }).catch(error => {
          reject(error)
        })
      })
    },

    // 获取用户信息
    GetUserInfo({ commit, state }) {
      return new Promise((resolve, reject) => {
        getUserInfo(state.token).then(response => {
          //     console.log(response,'毒瘤');
          //
          //     const data = response.data
          const data = {
            roles: [response.data.Position.id + ''],
            token: 'admin',
            introduction: '我是超级管理员',
            avatar: response.data.user.avatar,
            name: response.data.user.name
          }
          commit('SET_ROLES', data.roles)
          // if (data.roles && data.roles.length > 0) { // 验证返回的roles是否是一个非空数组
          //     commit('SET_ROLES', 'admin')
          // } else {
          //     reject('getInfo: roles must be a non-null array !')
          // }

          commit('SET_NAME', data.name)
          commit('SET_AVATAR', data.avatar)
          commit('SET_INTRODUCTION', data.introduction)
          resolve({ data })
        }).catch(error => {
          reject(error)
        })
      })
    },

    // 登出
    LogOut({commit, state}) {
      // return new Promise((resolve, reject) => {
      //     logout(state.token).then(() => {
      //         commit('SET_TOKEN', '')
      //         commit('SET_ROLES', [])
      //         removeToken()
      //         resolve()
      //     }).catch(error => {
      //         reject(error)
      //     })
      // })
      commit('SET_TOKEN', '')
      commit('SET_ROLES', [])
      removeToken()
    },

    // 前端 登出
    FedLogOut({ commit }) {
      return new Promise(resolve => {
        commit('SET_TOKEN', '')
        commit('SET_ROLES', [])
        removeToken()
        resolve()
      })
    },

    // 动态修改权限
    ChangeRoles({commit, dispatch}, role) {
      return new Promise(resolve => {
        commit('SET_TOKEN', role)
        setToken(role)
        getUserInfo(role).then(response => {
          const data = response.data
          commit('SET_ROLES', data.roles)
          commit('SET_NAME', data.name)
          commit('SET_AVATAR', data.avatar)
          commit('SET_INTRODUCTION', data.introduction)
          dispatch('GenerateRoutes', data) // 动态修改权限后 重绘侧边菜单
          resolve()
        })
      })
    }
  }
}

export default user
